package com.zhixing.map.controller;

import cn.hutool.core.util.ReUtil;
import com.zhixing.common.annotation.Driver;
import com.zhixing.common.annotation.Passenger;
import com.zhixing.common.domain.dto.LocationDTO;
import com.zhixing.common.domain.vo.DriverDistanceVO;
import com.zhixing.common.exception.ZhixingException;
import com.zhixing.common.utils.Res;
import com.zhixing.map.domain.dto.DriverLocationDTO;
import com.zhixing.map.service.LocationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.zhixing.common.domain.enums.HttpStatus.CITY_CODE_ERROR;

/**
 * 位置接口
 */
@Slf4j
@RestController
@RequestMapping("/location")
@RequiredArgsConstructor
public class LocationController {

    private final LocationService locationService;

    /**
     * 更新司机位置
     * @param driverLocationDTO 司机位置信息
     * @return 操作结果
     */
    @Driver
    @PutMapping("/driverLocation")
    public Res<Void> updateDriverLocation(@Validated @RequestBody DriverLocationDTO driverLocationDTO) {
        log.info("地图服务调用，更新司机位置，司机id：{}", driverLocationDTO.getDriverInfo().getDriverId());
        locationService.updateDriverLocation(driverLocationDTO);
        return Res.ok();
    }

    /**
     * 删除司机位置
     * @param cityCode 城市代码
     * @param driverId 司机id
     * @return 操作结果
     */
    @Driver
    @DeleteMapping("/driverLocation/{cityCode}/{driverId}")
    public Res<Void> deleteDriverLocation(@PathVariable String cityCode, @PathVariable Integer driverId) {
        log.info("地图服务调用，删除司机位置，司机id：{}", driverId);
        if (!ReUtil.isMatch("^\\d{4}$", cityCode)) throw new ZhixingException(CITY_CODE_ERROR);
        locationService.deleteDriverLocation(cityCode, driverId);
        return Res.ok();
    }

    /**
     * 获取订单附近司机
     * @param locationDTO 位置信息
     * @return List<DriverDistanceVO> 司机位置列表
     */
    @Passenger
    @GetMapping("/nearbyDrivers")
    public Res<List<DriverDistanceVO>> getOrderNearbyDrivers(@Validated LocationDTO locationDTO) {
        log.info("地图服务调用，获取订单附近司机");
        return Res.ok(locationService.getOrderNearbyDrivers(locationDTO));
    }
}
